package com.qs.eggs.integral.activity;

import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.CitySelectAdapter;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.refresh.utils.TDevice;
import com.qs.eggs.utils.CityDialogHelper;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class SelectAddressActivity extends BaseActivity implements View.OnClickListener{
    private EditText name,phone;
    private  Button baocun;
    public EditText address;
    private LinearLayout mProvince,mCity,mCounty,mCommunity;
    public TextView tvProvince,tvCity,tvCounty,tvCommunity;
    private ArrayList<HashMap<String,String>> mList;
    View mCityView;
    RecyclerView mCityRecyclerView;
    AlertDialog dialog;
    private TextView tvForIdP,tvForIdCity,tvForIdCounty,tvForIdCommunity;
    private String ProvinceId="",CityId="",CountyId="",CommunityId="",HeaderId="";
    private String xingming,number;

    private String orderNo,buniessid;
    public static SelectAddressActivity instance;


    @Override
    protected int getContentView() {
        return R.layout.integral_activity_add_address;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        instance=this;
        orderNo=getIntent().getStringExtra("orderno");
        initView();
        getInfo();
    }

    private void initView(){
        name=(EditText)findViewById(R.id.et_name);
        phone=(EditText)findViewById(R.id.et_phone);
        address=(EditText)findViewById(R.id.et_address);

        tvProvince=(TextView)findViewById(R.id.tv_province);
        tvCity=(TextView)findViewById(R.id.tv_city);
        tvCounty=(TextView)findViewById(R.id.tv_county);
        tvCommunity=(TextView)findViewById(R.id.tv_community);

        tvForIdP=(TextView)findViewById(R.id.tv_forid_p);
        tvForIdCity=(TextView)findViewById(R.id.tv_forid_city);
        tvForIdCounty=(TextView)findViewById(R.id.tv_forid_county);
        tvForIdCommunity=(TextView)findViewById(R.id.tv_forid_ommunity);

        mProvince=(LinearLayout)findViewById(R.id.lly_province);
        mCity=(LinearLayout)findViewById(R.id.lly_city);
        mCounty=(LinearLayout)findViewById(R.id.lly_county);
        mCommunity=(LinearLayout)findViewById(R.id.lly_community);

        baocun=(Button)findViewById(R.id.save);
        mProvince.setOnClickListener(this);
        mCity.setOnClickListener(this);
        mCounty.setOnClickListener(this);
        mCommunity.setOnClickListener(this);
        baocun.setOnClickListener(this);
    }
    //获得个人信息
    private void getInfo(){
        OkGo.post(Constants.URL_BASE + "personal?action=getPersonalInfo")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("个人信息==" + s);
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                xingming = jsonArray.getJSONObject(i).getString("name");
                                number = jsonArray.getJSONObject(i).getString("phone");
                                name.setText(xingming);
                                phone.setText(number);

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lly_province:
            HttpApi.getProvince(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    try {
                        ArrayList<String> mProvincesList=new ArrayList<String>();
                        ArrayList<String> mProvincesListId=new ArrayList<String>();
                        JSONArray jsonArray=new JSONArray(s);
                        for (int i=0;i<jsonArray.length();i++){
                            mProvincesList.add(jsonArray.getJSONObject(i).getString("areaname"));
                            mProvincesListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                        }
                        mCityView = LayoutInflater.from(SelectAddressActivity.this).inflate(R.layout.dialog_recyclerview_city, null, false);

                        mCityRecyclerView=(RecyclerView)mCityView.findViewById(R.id.rv_city);
                        CitySelectAdapter provincesAdapter=new CitySelectAdapter(SelectAddressActivity.this,mProvincesListId,mProvincesList);
                        mCityRecyclerView.setAdapter(provincesAdapter);
                        mCityRecyclerView.setLayoutManager(new LinearLayoutManager(SelectAddressActivity.this));
                        provincesAdapter.setOnItemClickLitener(new CitySelectAdapter.OnItemClickLitener() {
                            @Override
                            public void onItemClick(View view, int position, String id, String name) {
                                dialog.dismiss();
                                ProvinceId=id;
                                tvProvince.setText(name);
                                tvCity.setText("");
                                tvCounty.setText("");
                                tvCommunity.setText("");
                                address.setText("");
                            }

                            @Override
                            public void onItemLongClick(View view, int position) {

                            }
                        });
                        final AlertDialog.Builder builder = new AlertDialog.Builder(SelectAddressActivity.this);
                        builder
                                .setView(mCityView);
                        dialog=builder.create();
                        dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
                        dialog.show();
                        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                        params.width =(int)(TDevice.getScreenWidth()*0.8);
                        params.height = (int)(TDevice.getScreenHeight()*0.7) ;
                        dialog.getWindow().setAttributes(params);

                        //  CityDialogHelper.getCity(SelectAddressActivity.this,mProvincesList,mProvincesListId,tvProvince,tvForIdP);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            break;
            //加载市列表
            case R.id.lly_city:
                if(tvProvince.getText().toString().trim().length()==0){
                    Toast.makeText(SelectAddressActivity.this,"请先选择省",Toast.LENGTH_SHORT).show();
                    return;
                }
               // ProvinceId=tvForIdP.getText().toString();
                HttpApi.getCity(ProvinceId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCityList=new ArrayList<String>();
                            ArrayList<String> mCityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }


                            mCityView = LayoutInflater.from(SelectAddressActivity.this).inflate(R.layout.dialog_recyclerview_city, null, false);

                            mCityRecyclerView=(RecyclerView)mCityView.findViewById(R.id.rv_city);
                            CitySelectAdapter provincesAdapter=new CitySelectAdapter(SelectAddressActivity.this,mCityListId,mCityList);
                            mCityRecyclerView.setAdapter(provincesAdapter);
                            mCityRecyclerView.setLayoutManager(new LinearLayoutManager(SelectAddressActivity.this));
                            provincesAdapter.setOnItemClickLitener(new CitySelectAdapter.OnItemClickLitener() {
                                @Override
                                public void onItemClick(View view, int position, String id, String name) {
                                    dialog.dismiss();
                                    CityId=id;
                                    tvCity.setText(name);
                                    tvCounty.setText("");
                                    tvCommunity.setText("");
                                    address.setText("");
                                }

                                @Override
                                public void onItemLongClick(View view, int position) {

                                }
                            });
                            final AlertDialog.Builder builder = new AlertDialog.Builder(SelectAddressActivity.this);
                            builder
                                    .setView(mCityView);
                            dialog=builder.create();
                            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
                            dialog.show();
                            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                            params.width =(int)(TDevice.getScreenWidth()*0.8);
                            params.height = (int)(TDevice.getScreenHeight()*0.7) ;
                            dialog.getWindow().setAttributes(params);

                          //  CityDialogHelper.getCity(SelectAddressActivity.this,mCityList,mCityListId,tvCity,tvForIdCity);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }


                });
                break;

            //加载县列表
            case R.id.lly_county:
                if(tvCity.getText().toString().trim().length()==0){
                    Toast.makeText(SelectAddressActivity.this,"请先选择市",Toast.LENGTH_SHORT).show();
                    return;
                }
              //  CityId=tvForIdCity.getText().toString();
                HttpApi.getCounty(CityId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCountyList=new ArrayList<String>();
                            ArrayList<String> mCountyListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCountyList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCountyListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            mCityView = LayoutInflater.from(SelectAddressActivity.this).inflate(R.layout.dialog_recyclerview_city, null, false);

                            mCityRecyclerView=(RecyclerView)mCityView.findViewById(R.id.rv_city);
                            CitySelectAdapter provincesAdapter=new CitySelectAdapter(SelectAddressActivity.this,mCountyListId,mCountyList);
                            mCityRecyclerView.setAdapter(provincesAdapter);
                            mCityRecyclerView.setLayoutManager(new LinearLayoutManager(SelectAddressActivity.this));
                            provincesAdapter.setOnItemClickLitener(new CitySelectAdapter.OnItemClickLitener() {
                                @Override
                                public void onItemClick(View view, int position, String id, String name) {
                                    dialog.dismiss();
                                    CountyId=id;
                                    tvCounty.setText(name);
                                    tvCommunity.setText("");
                                    address.setText("");
                                }

                                @Override
                                public void onItemLongClick(View view, int position) {

                                }
                            });
                            final AlertDialog.Builder builder = new AlertDialog.Builder(SelectAddressActivity.this);
                            builder
                                    .setView(mCityView);
                            dialog=builder.create();
                            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
                            dialog.show();
                            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                            params.width =(int)(TDevice.getScreenWidth()*0.8);
                            params.height = (int)(TDevice.getScreenHeight()*0.7) ;
                            dialog.getWindow().setAttributes(params);

                            //  CityDialogHelper.getCity(SelectAddressActivity.this,mCountyList,mCountyListId,tvCounty,tvForIdCounty);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            //加载小区列表
            case R.id.lly_community:
                if(tvCounty.getText().toString().trim().length()==0){
                    Toast.makeText(SelectAddressActivity.this,"请先选择县",Toast.LENGTH_SHORT).show();
                    return;
                }
             //   CountyId=tvForIdCounty.getText().toString();

                HttpApi.getShangjia(CountyId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCommunityList=new ArrayList<String>();
                            ArrayList<String> mCommunityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCommunityList.add(jsonArray.getJSONObject(i).getString("businessname"));
                                mCommunityListId.add(jsonArray.getJSONObject(i).getString("id"));
                            }

                            mCityView = LayoutInflater.from(SelectAddressActivity.this).inflate(R.layout.dialog_recyclerview_city, null, false);

                            mCityRecyclerView=(RecyclerView)mCityView.findViewById(R.id.rv_city);
                            CitySelectAdapter provincesAdapter=new CitySelectAdapter(SelectAddressActivity.this,mCommunityListId,mCommunityList);
                            mCityRecyclerView.setAdapter(provincesAdapter);
                            mCityRecyclerView.setLayoutManager(new LinearLayoutManager(SelectAddressActivity.this));
                            provincesAdapter.setOnItemClickLitener(new CitySelectAdapter.OnItemClickLitener() {
                                @Override
                                public void onItemClick(View view, int position, String id, String name) {
                                    dialog.dismiss();
                                    buniessid=id;
                                    tvCommunity.setText(name);
                                    HttpApi.getShangjiaDetail(id, new StringCallback() {
                                        @Override
                                        public void onSuccess(String s, Call call, Response response) {
                                            address.setText(s);

                                        }
                                    });

                                }

                                @Override
                                public void onItemLongClick(View view, int position) {

                                }
                            });
                            final AlertDialog.Builder builder = new AlertDialog.Builder(SelectAddressActivity.this);
                            builder
                                    .setView(mCityView);
                            dialog=builder.create();
                            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
                            dialog.show();
                            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                            params.width =(int)(TDevice.getScreenWidth()*0.8);
                            params.height = (int)(TDevice.getScreenHeight()*0.7) ;
                            dialog.getWindow().setAttributes(params);
                         //   CityDialogHelper.getCity(SelectAddressActivity.this,mCommunityList,mCommunityListId,tvCommunity,tvForIdCommunity);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }

                });
                CommunityId=tvCommunity.getText().toString();
                break;
            case R.id.save:
                save();
                break;
        }
    }

    //提交兑换
    private void save(){
        if(tvProvince.getText().length()==0||
                tvCity.getText().length()==0||
                tvCounty.getText().length()==0||
                tvCommunity.getText().length()==0||
                address.getText().length()==0){

            Toast.makeText(SelectAddressActivity.this,"请填写完整信息",Toast.LENGTH_SHORT).show();
            return;
        }
        HashMap<String,String> map=new HashMap<String, String>();
                map.put("orderno",orderNo);
                map.put("buniessid",buniessid);
                Gson gson=new Gson();
                String data=gson.toJson(map);
                OkGo.post(Constants.URL_BASE+"changezoneServlet?action=lijiduihuan")
                        .params("params",data)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {

                                if(s.equals("\"true\"")){
                                    Toast.makeText(SelectAddressActivity.this,"兑换成功",Toast.LENGTH_SHORT).show();

                                    ExchangeOrderActivity.getList();
                                    finish();
                                }else {
                                    Toast.makeText(SelectAddressActivity.this,"兑换失败",Toast.LENGTH_SHORT).show();

                                }
                            }
                        });
      }
}